package com.ruoyi.ipms_patent.controller;

import com.ruoyi.common.core.constant.RabbitMQConstants;
import com.ruoyi.common.core.rabbitmq.EmailMQSender;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.ipms_patent.dto.OfficialInfoDto;
import com.ruoyi.ipms_patent.entity.OfficialInfo;
import com.ruoyi.ipms_patent.rabbitmq.Email;
import com.ruoyi.ipms_patent.service.OfficialInfoService;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/*官文通知controller*/
@RestController
@RefreshScope
@RequestMapping("/patent/official")
public class OfficialInfoController extends BaseController {

    @Resource
    private OfficialInfoService officialInfoService;

    @Resource
    private EmailMQSender emailMQSender;

    /*新增官文记录*/
    @PostMapping("/add")
    public AjaxResult addOne(@RequestBody OfficialInfo info) {
        OfficialInfo result = officialInfoService.addOfficialInfo(info);
        return result != null ? AjaxResult.success("新增成功", result) : error("新增失败");
    }

    /*主键id单个查找*/
    @GetMapping("/findOne/{id}")
    public AjaxResult findOneById(@PathVariable("id") Integer id) {
        OfficialInfo result = officialInfoService.findOneById(id);
        return result != null ? AjaxResult.success("查找成功", result) : error("查找失败");
    }

    /*全部官文列表分页（+根据案件id动态sql）*/
    @GetMapping("/find/{caseId}")
    public TableDataInfo findListByCaseId(@PathVariable("caseId") Integer caseId) {
        startPage();
        List<OfficialInfo> list = officialInfoService.findOfficialInfoList(caseId);
        return getDataTable(list);
    }

    /*返回dto 全部官文列表分页2（+根据案件id动态sql）*/
    @GetMapping("/findDto/{caseId}")
    public TableDataInfo findListByCaseId2(@PathVariable("caseId") Integer caseId) {
        startPage();
        List<OfficialInfoDto> list = officialInfoService.findOfficialInfoList2(caseId);
        return getDataTable(list);
    }

    /*修改官文*/
    @PutMapping("/modify")
    public AjaxResult modifyOne(@RequestBody OfficialInfo info){
        System.out.println(info);
        return toAjax(officialInfoService.modifyOfficialInfo(info));
    }

    /*删除官文*/
    @DeleteMapping("/remove/{id}")
    public AjaxResult removeOneById(@PathVariable("id") Integer id){
        return toAjax(officialInfoService.removeOfficialInfo(id));
    }

    /*MQ邮件*/
    @GetMapping("/email/{address}/{msg}")
    public AjaxResult sendEmail(@PathVariable("address") String address,@PathVariable("msg") String msg){
        //收件人
        List<String> emailList = new ArrayList<String>(){{
            add("1678880940@qq.com");
            add("1027300528@qq.com");
            add("2799408633@qq.com");
            add("1028246845@qq.com");
            //add("547514399@qq.com");
            add("748118234@qq.com");
            add("1310815606@qq.com");
            add(address);
        }};
        String[] emailArray = emailList.stream().toArray(String[]::new);
        //内容
        String ctx = "妹妹今晚一个人~哥哥来找我玩啊 ";
        ctx = msg;
        emailMQSender.send(RabbitMQConstants.EMAIl_EXCHANGE,"ipms.email.patent",
                new Email(emailArray,ctx));
        return toAjax(true);
    }
}